package com.ruoyi.system.service.impl;

import java.time.LocalDate;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RaintestrecordMapper;
import com.ruoyi.system.domain.Raintestrecord;
import com.ruoyi.system.service.IRaintestrecordService;


/**
 * 雨量计任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-01
 */
@Service
public class RaintestrecordServiceImpl implements IRaintestrecordService 
{
    @Autowired
    private RaintestrecordMapper raintestrecordMapper;

    /**
     * 查询雨量计任务
     * 
     * @param RegisterID 雨量计任务主键
     * @return 雨量计任务
     */
    @Override
    public Raintestrecord selectRaintestrecordByRegisterID(String RegisterID)
    {
        return raintestrecordMapper.selectRaintestrecordByRegisterID(RegisterID);
    }

    /**
     * 查询雨量计任务列表
     * 
     * @param raintestrecord 雨量计任务
     * @return 雨量计任务
     */
    @Override
    public List<Raintestrecord> selectRaintestrecordList(Raintestrecord raintestrecord)
    {
        return raintestrecordMapper.selectRaintestrecordList(raintestrecord);
    }

    /**
     * 新增雨量计任务
     * 
     * @param raintestrecord 雨量计任务
     * @return 结果
     */
    @Override
    public int insertRaintestrecord(Raintestrecord raintestrecord) {
        LocalDate currentDate = LocalDate.now();
        {
            String currentYear = currentDate.getYear() + "";
            String taskid = raintestrecordMapper.selectMaxTaskId(currentYear);
            if (taskid == null) {
                taskid = currentYear + "0001";
            } else {
                String Add = incrementString(taskid.substring(4), 4);
                taskid = currentYear + Add;
            }

            String RegisterID = raintestrecordMapper.selectMaxRegisterID(currentYear);
            if (RegisterID == null) {
                RegisterID = currentYear + "000001";
            } else {
                String Add = incrementString(taskid.substring(4), 6);
                RegisterID = currentYear + Add;
            }
            raintestrecord.setTaskID(taskid);
            raintestrecord.setRegisterID(RegisterID);
            raintestrecord.setTestState("2");
            return raintestrecordMapper.insertRaintestrecord(raintestrecord);
        }
    }
    /**
     * 修改雨量计任务
     * 
     * @param raintestrecord 雨量计任务
     * @return 结果
     */
    @Override
    public int updateRaintestrecord(Raintestrecord raintestrecord)
    {
        return raintestrecordMapper.updateRaintestrecord(raintestrecord);
    }

    /**
     * 批量删除雨量计任务
     * 
     * @param RegisterIDs 需要删除的雨量计任务主键
     * @return 结果
     */
    @Override
    public int deleteRaintestrecordByRegisterIDs(String[] RegisterIDs)
    {
        return raintestrecordMapper.deleteRaintestrecordByRegisterIDs(RegisterIDs);
    }

    /**
     * 删除雨量计任务信息
     * 
     * @param RegisterID 雨量计任务主键
     * @return 结果
     */
    @Override
    public int deleteRaintestrecordByRegisterID(String RegisterID)
    {
        return raintestrecordMapper.deleteRaintestrecordByRegisterID(RegisterID);
    }



        public static String incrementString(String value, int length) {
            // 将字符串转换为整数
            int intValue = Integer.parseInt(value);

            // 对整数加 1
            intValue++;

            // 将新的整数转换为字符串
            String newValue = Integer.toString(intValue);

            // 补零，确保字符串长度不变
            while (newValue.length() < length) {
                newValue = "0" + newValue;
            }

            return newValue;
        }


}
